package GUI_Data.FlightSeatPanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import Data_Flow.DataOperator;
import Data_Flow.GetData;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * The seat button.
 *
 * @author Xiaorun Wang
 * @since 9 April 2022
 * @version 1.0
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class SeatButton extends JButton implements ActionListener {

    private GetData getData;
    private DataOperator dataOperator;

    /**
     * The class of the seat.
     * Use enum in Data_Structure.DataImplement.SeatClass
     */
    private String seatClass;

    /**
     * The price of the seat.
     */
    private int seatPrice;

    /**
     * The row number of the seat.
     */
    private String row;

    /**
     * The column number of the seat.
     */
    private String column;

    /**
     * The seat position of the seat.
     * Use enum in Data_Structure.DataImplement.SeatPosition
     */
    private String seatPosition;

    /**
     * Whether the seat has been booked by others or not.
     */
    private boolean ifSelectable;

    /**
     * The FlightSeatPanel which generate this button.
     * Used for calling the select method in it.
     */
    private FlightSeatPanel parentPanel;

    /**
     * The constructor of SeatButton.
     * The color will be added according to ifSelectable.
     * @param row The row number of the button.
     * @param column The column number of the button.
     * @param seatClass The seat class of the seat.
     * @param parentPanel The panel the button belongs to.
     */
    public SeatButton(String row, String column, String seatClass, FlightSeatPanel parentPanel, GetData getData, DataOperator dataOperator){
        this.seatPrice = GetData.getSeatPrice(getData.getMyPlaneType(),seatClass);
        this.row = row;
        this.column = column;
        this.seatPosition = column + row;
        this.seatClass = seatClass;
        this.parentPanel = parentPanel;
        this.getData = getData;
        this.dataOperator = dataOperator;
        this.ifSelectable = getData.ifSeatSelectable(this);
        //this.ifSelectable = ifSeatSelectable(this);
        this.setText(seatPosition);

        if (this.ifSelectable){
            this.setBackground(Color.WHITE);
        }else {
            this.setBackground(Color.RED);
        }
        this.addActionListener(this);
    }

    /**
     * When this button is clicked.
     * @param e This class.
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        if(this.ifSelectable){
            this.parentPanel.seatSelect(this);
        }
    }
}
